package com.weitu.emergency.service.impl;

import com.weitu.emergency.entity.RehearsalInfo;
import com.weitu.emergency.entity.RehearsalPlan;
import com.weitu.emergency.entity.RehearsalUser;
import com.weitu.emergency.entity.RehearsalUserOther;
import com.weitu.emergency.mapper.RehearsalInfoMapper;
import com.weitu.emergency.mapper.RehearsalPlanMapper;
import com.weitu.emergency.mapper.RehearsalUserMapper;
import com.weitu.emergency.mapper.RehearsalUserOtherMapper;
import com.weitu.emergency.service.RehearsalPlanService;
import com.weitu.task.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-05-10
 */
@Service
public class RehearsalPlanServiceImpl implements RehearsalPlanService {

    private final RehearsalPlanMapper rehearsalPlanMapper;
    private final RehearsalInfoMapper rehearsalInfoMapper;
    private final RehearsalUserMapper rehearsalUserMapper;
    private final RehearsalUserOtherMapper rehearsalUserOtherMapper;
    private final TaskMapper taskMapper;

    @Autowired
    public RehearsalPlanServiceImpl(RehearsalPlanMapper rehearsalPlanMapper, RehearsalInfoMapper rehearsalInfoMapper, RehearsalUserMapper rehearsalUserMapper, RehearsalUserOtherMapper rehearsalUserOtherMapper, TaskMapper taskMapper) {
        this.rehearsalPlanMapper = rehearsalPlanMapper;
        this.rehearsalInfoMapper = rehearsalInfoMapper;
        this.rehearsalUserMapper = rehearsalUserMapper;
        this.rehearsalUserOtherMapper = rehearsalUserOtherMapper;
        this.taskMapper = taskMapper;
    }

    @Override
    public List<RehearsalPlan> findByParam(Map paramMap) {

        return rehearsalPlanMapper.findByParam(paramMap);
    }

    @Override
    public Integer findByParamCount(Map paramMap) {
        return rehearsalPlanMapper.findByParamCount(paramMap);
    }

    @Override
    public RehearsalPlan findById(String id) {

        RehearsalPlan rehearsalPlan = rehearsalPlanMapper.findById(id);
        //查询信息
        rehearsalPlan.setRehearsalInfo(rehearsalInfoMapper.findByRehearsalPlanId(id));
        //查询用户
        rehearsalPlan.setRehearsalUserOther(rehearsalUserOtherMapper.findByRehearsalPlanId(id));

        return rehearsalPlan;
    }

    @Transactional
    @Override
    public Integer add(RehearsalPlan rehearsalPlan, List<RehearsalUser> rehearsalUserList) {

        if (null != rehearsalUserList) {
            if (rehearsalUserList.size() > 0) {
                //保存参演人员
                rehearsalUserMapper.add(rehearsalUserList);
            }
        }
        RehearsalUserOther rehearsalUserOther = rehearsalPlan.getRehearsalUserOther();
        if (null != rehearsalUserOther) {
            //保存企外参演人员
            rehearsalUserOtherMapper.add(rehearsalUserOther);
        }

        RehearsalInfo rehearsalInfo = rehearsalPlan.getRehearsalInfo();
        if (null != rehearsalInfo) {
            //保存基本信息
            rehearsalInfoMapper.add(rehearsalInfo);
        }
        return rehearsalPlanMapper.add(rehearsalPlan);
    }

    @Transactional
    @Override
    public Integer updateById(RehearsalPlan rehearsalPlan, List<RehearsalUser> rehearsalUserList,Map record, String id) {

        //删除计划信息
        rehearsalInfoMapper.deleteByRehearsalPlanId(id);
        //删除参演人员
        rehearsalUserMapper.deleteByRehearsalPlanId(id);
        //删除企外参演人员
        rehearsalUserOtherMapper.deleteByRehearsalPlanId(id);
        //删除问题
        taskMapper.deleteTrueByBusinessId(id);

        if (null != rehearsalUserList) {
            if (rehearsalUserList.size() > 0) {
                //保存参演人员
                rehearsalUserMapper.add(rehearsalUserList);
            }
        }
        RehearsalUserOther rehearsalUserOther = rehearsalPlan.getRehearsalUserOther();
        if (null != rehearsalUserOther) {
            //保存企外参演人员
            rehearsalUserOtherMapper.add(rehearsalUserOther);
        }

        RehearsalInfo rehearsalInfo = rehearsalPlan.getRehearsalInfo();
        if (null != rehearsalInfo) {
            //保存基本信息
            rehearsalInfoMapper.add(rehearsalInfo);
        }

        taskMapper.insert(record);
        return rehearsalPlanMapper.updateById(rehearsalPlan);
    }

    @Transactional
    @Override
    public Integer deleteById(Map paramMap,String id) {
        //删除计划信息
        rehearsalInfoMapper.deleteByRehearsalPlanId(id);
        //删除参演人员
        rehearsalUserMapper.deleteByRehearsalPlanId(id);
        //删除企外参演人员
        rehearsalUserOtherMapper.deleteByRehearsalPlanId(id);

        return rehearsalPlanMapper.deleteById(paramMap);
    }
}
